package grid;

import grid.items.Body;
import grid.items.Tile;

import java.awt.Point;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;

public class ShootingGridModel extends GridModel {
	public ShootingGridModel(int x, int y) {
		super(x, y);
	}

	public Set<Body> executeShots(Queue<Couple<Body, Point>> shot) {
		Set<Body>killed=new HashSet<Body>();
		for(Couple<Body,Point> target : shot)
		{
			Body shooter=target.x;
			if(shooter.isDead())
				System.out.println(shooter+" is dead but intends to shoot");
			else{
			Point position=getPosition(shooter);
			if(position.distance(target.y)>5)
				System.out.println(target.x+" shoots in "+target.y+" that is out of his range ");
			else
			{
				if(!isBody(target.y))
					System.out.println(shooter+" shoots in no-one in "+target.y);
				else
				{
					Body shooted=getBody(target.y);
					//Edit: Frank//
					if(shooted == null)
						System.out.println("target is null, " + target.y);
					else
						System.out.println("target is ok" + target.y);
					//Edit:Frank//
					if(shooter.dominates(shooted))
						shooted.inflictInjure(3);
					else 
						shooted.inflictInjure(1);
					if(shooted.isDead())
						killed.add(shooted);
				}
			}
			}
		}
		
		for(Body b:killed)
			remove(b);
		
		shot.clear();
		return killed;
	}


	/*private void remove(Set<Body> killed) {
		for(Body b:killed)
			remove(b);
	}*/
}
